/*
 *    Copyright 2019 sclimin
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.sclimin.pickerview;

import android.content.Context;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

/**
 * 作者：limin
 * <p>
 * 创建时间：2019/1/24
 */
public class CameraView extends View {

    private final Camera mCamera = new Camera();
    private final Matrix mMatrix = new Matrix();
    private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private final RectF mRect = new RectF();

    private final float mDensity = getContext().getResources()
            .getDisplayMetrics().density;

    private float mOffsetX;
    private float mOffsetY;

    private final GestureDetector mDetector = new GestureDetector(getContext(),
            new GestureDetector.SimpleOnGestureListener() {
                @Override
                public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                    mOffsetX -= distanceX;
                    mOffsetY -= distanceY;
                    postInvalidate();
                    return true;
                }
            });

    public CameraView(Context context) {
        this(context, null);
    }

    public CameraView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        View parent = (View) getParent();

        int offsetCenter = (int) (parent.getMeasuredWidth() * 0.5f - getLeft());

        mRect.set(0, 0, getMeasuredWidth(), mDensity * 200);
        mRect.offset(0, getMeasuredHeight() * 0.5f - mRect.centerY());


//        mPaint.reset();
//        mPaint.setStyle(Paint.Style.STROKE);
//        mPaint.setStrokeWidth(mDensity);
//        mPaint.setColor(Color.BLACK);
//        canvas.drawRect(mRect, mPaint);
//
//        mPaint.setStrokeWidth(3*mDensity);
//        mPaint.setColor(Color.RED);
//        canvas.drawPoint(mOffsetX, getMeasuredHeight() * 0.5f, mPaint);

        canvas.save();
        canvas.translate(mRect.left, mRect.top);
        mRect.offsetTo(0 ,0);

        mPaint.reset();
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(Color.parseColor("#8000ff00"));

        mCamera.save();
        mCamera.translate(0, 0, 10 * mDensity);
        mCamera.rotateX(mOffsetY);
        mCamera.getMatrix(mMatrix);
        mCamera.restore();

        mMatrix.preTranslate(-offsetCenter, -mRect.height() * 0.5f);
        mMatrix.postTranslate(offsetCenter, mRect.height() * 0.5f);

        canvas.concat(mMatrix);
        canvas.drawRect(mRect, mPaint);

//        mPaint.setColor(Color.BLUE);
//        canvas.drawText("ABC", 0, 0, mPaint);

        canvas.restore();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mDetector.onTouchEvent(event);
        return true;
    }
}
